define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/agent/index',
                    add_url: 'user/agent/add',
                    edit_url: 'user/agent/edit',
                    del_url: 'user/agent/del',
                    multi_url: 'user/agent/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {field: 'group.name', title: __('Group'),operate:false},
                        {field: 'nickname', title: '姓名（昵称）', operate: 'LIKE'},
                        {field: 'username', title: '代理账号', operate: 'LIKE'},
                        {field: 'code', title: '邀请码', operate: false},
                        {field: 'mobile', title: '手机号', operate: 'LIKE'},
                        {field: 'rebateNum', title: '返点数', operate: false},
                        {field: 'logintime', title: '登录时间', formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: '登录ip', formatter: Table.api.formatter.search},
                        {field: 'jointime', title: '注册时间', formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {
                            field: 'pid',
                            title: '层级',
                            formatter: function (value, row, index) {
                                return '<a href="javascript:;" class="searchgrade" data-pid="'+ row.pid +'" data-field="pid" data-value="' + row.id + '">' + '层级' + '</a>';
                            },
                            operate: false
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on('click','.searchgrade',function () {
                var pid = $(this).attr('data-value');
                table.bootstrapTable('refresh', {url: 'user/agent?pid='+pid});
                return false;

            });
            $(document).on('click','.upgrade',function () {
                table.bootstrapTable('refresh', {url: 'user/agent'});
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});